package com.lagou.mall.esflow.server.handler;

import com.lagou.mall.esflow.server.config.SpringUtil;
import com.lagou.mall.esflow.server.consumer.IIndexConsumer;
import java.util.Map;
import java.util.Map.Entry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * @Author xiazhenyu
 * @Description
 * @Date 5:37 下午 2021/2/24
 **/
@Slf4j
@Configuration
public class ConsumerHandlerBuilder implements ApplicationListener<ContextRefreshedEvent> {


    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (null == contextRefreshedEvent.getApplicationContext().getParent()) {
            Map<String, IIndexConsumer> beanMap =
                    SpringUtil.getApplicationContext().getBeansOfType(IIndexConsumer.class);
            for (Entry<String, IIndexConsumer> entry : beanMap.entrySet()) {
                //调用handler中的topicProcess 方法
                Thread thread = new Thread(() -> {
                    TopicConsumerProcess topicConsumerProcess = new TopicConsumerProcess();
                    topicConsumerProcess.topicProcess(entry.getValue());
                });
                thread.setName(entry.getValue().getClass().getSimpleName() + "_BaseThread");
                thread.start();
                log.info("init success...");
            }

            log.info("加载ConsumerHandlerBuilder完成！！");
        }
    }
}
